package com.litbw.gui;

import javax.swing.JList;
import javax.swing.ListSelectionModel;

import com.litbw.dbbackend.DBEventAdapter;
import com.litbw.event.Event;

/**
 * The EventList is an extended version of JList which accepts only Event
 * objects as it's data. It internally stores the ListData but DOES provide
 * a method for retrieving a value from the current data set. It also uses
 * a custom cell renderer to make it look like our prototypes.
 * @author Michael Morris
 *
 */
public class EventList extends JList {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = -1853903696794800071L;

	/**
	 * Static instance of the custom renderer.
	 */
	private static EventCellRenderer _renderer_instance = null;
	
	/**
	 * The currently loaded list of Events.
	 */
	private Event[] list_data = null;
	
	/**
	 * Create a new EventList to display Events in the database.
	 */
	public EventList() {
		
		super();
		
		// Setup custom renderer.
		if (_renderer_instance == null)
			_renderer_instance = new EventCellRenderer();
		setCellRenderer(_renderer_instance);
		
		// Setup look
		setLayoutOrientation(JList.HORIZONTAL_WRAP);
		setVisibleRowCount(-1);
		setBorder(null);
		setBackground(null);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		updateEventData();
		
	}
	
	/**
	 * Retrieve a REFERENCE to the Event at the given index.
	 * @param index An index of the event to retrieve from the list.
	 * @return An Event, or null if index is out of bounds.
	 */
	public Event getEventAt(final int index) {
		
		Event event = null;
		
		if (index >= 0 && list_data != null && index < list_data.length)
			event = list_data[index];
		
		return event;
		
	}
	
	/**
	 * Update the lists data to whatever is currently inside the Event table
	 * of the database.
	 */
	public void updateEventData() {
		
		list_data = DBEventAdapter.getAllEvents();
		if (list_data != null)	
			setListData(list_data);
		
		
		
	}
	
}
